# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=octopi

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
#_qmake=${MINGW_PREFIX}/bin/qmake
_qmake=${MINGW_PREFIX}/qt5-static/bin/qmake
pkgver=r612.784de21
pkgrel=1
pkgdesc="a powerful Pacman frontend using Qt libs"
arch=('any')
url="https://github.com/aarnt/octopi"
license=('GPL2')
makedepends=('git' "${MINGW_PACKAGE_PREFIX}-qt5-static" "${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-make")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-git")
_gitroot="git://github.com/aarnt/octopi.git"
_gitname="octopi"
options=('!debug' 'strip')
source=("${_gitname}"::"${_gitroot}"
        "0001-Hack-isRootRunning-to-always-be-false-for-_WIN32.patch"
        "0002-Define-QT_WA-as-unicode.patch"
        "0003-Use-Q_PID-for-m_pidTerminal.patch"
        "0004-Hack-out-ProcessWrapper-onSingleShot-to-do-nothing-o.patch"
        "0005-Hardcode-relative-path-to-sh.exe-as-getSUCommand-for.patch")
md5sums=('SKIP'
         'b47603b49e9be3bbfa2478b6763c010f'
         '5cd5815cdcb9765bc2769da4359101eb'
         'e0a6dd9656b45ac2378ff2a74e5e57e2'
         '58a1329d2a6f4ff15cf8001caebdc915'
         '1b0215f9a1790ed833c8ada4550a95e3')

pkgver() {
    cd "$srcdir/$_realname"
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "$srcdir/$_gitname"
  git am "${srcdir}"/0001-Hack-isRootRunning-to-always-be-false-for-_WIN32.patch
  git am "${srcdir}"/0002-Define-QT_WA-as-unicode.patch
  git am "${srcdir}"/0003-Use-Q_PID-for-m_pidTerminal.patch
  git am "${srcdir}"/0004-Hack-out-ProcessWrapper-onSingleShot-to-do-nothing-o.patch
  git am "${srcdir}"/0005-Hardcode-relative-path-to-sh.exe-as-getSUCommand-for.patch
}

build() {
  local _config
  cd ${srcdir}
  if check_option "debug" "n"; then
    _config="release"
  else
    _config="debug"
  fi

  rm -rf build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  msg "Building Octopi..."
  ${_qmake} ../$_gitname/$_gitname.pro CONFIG+="c++11 ${_config}"
  mingw32-make ${_config}

  mkdir -p notifier/pacmanhelper
  pushd notifier/pacmanhelper
  msg "Building pacmanhelper..."
  ${_qmake} ../../../$_gitname/notifier/pacmanhelper/pacmanhelper.pro CONFIG+="${_config}"
  mingw32-make ${_config}
  popd

  mkdir -p notifier/$_gitname-notifier
  pushd notifier/$_gitname-notifier
  msg "Building $_gitname-notifier..."
  ${_qmake} ../../../$_gitname/notifier/$_gitname-notifier/$_gitname-notifier.pro CONFIG+="release"
  mingw32-make ${_config}
  popd
}

package() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  PREFIX_WIN=$(pwd -W)
  popd > /dev/null

  cd "$srcdir/build-${MINGW_CHOST}"

  install -D -m755 bin/$_gitname                                          ${pkgdir}${MINGW_PREFIX}/bin/$_gitname
  install -D -m755 notifier/bin/$_gitname-notifier                        ${pkgdir}${MINGW_PREFIX}/bin/$_gitname-notifier
  install -D -m644 $srcdir/$_gitname/resources/images/${_gitname}_red.png ${pkgdir}${MINGW_PREFIX}/share/icons/$_gitname.png

  #services
  install -D -m755 notifier/bin/pacmanhelper                                    ${pkgdir}${MINGW_PREFIX}/lib/octopi/pacmanhelper

  install -D -m644 $srcdir/$_gitname/notifier/pacmanhelper/polkit/org.octopi.pacman.policy        ${pkgdir}${MINGW_PREFIX}/share/polkit-1/actions/org.octopi.pacman.policy
  install -D -m644 $srcdir/$_gitname/notifier/pacmanhelper/polkit/org.octopi.pacmanhelper.conf    ${pkgdir}${MINGW_PREFIX}/etc/dbus-1/system.d/org.octopi.pacmanhelper.conf
  install -D -m644 $srcdir/$_gitname/notifier/pacmanhelper/polkit/org.octopi.pacmanhelper.xml     ${pkgdir}${MINGW_PREFIX}/share/dbus-1/interfaces/org.octopi.pacmanhelper.xml
  install -D -m644 $srcdir/$_gitname/notifier/pacmanhelper/polkit/org.octopi.pacmanhelper.service ${pkgdir}${MINGW_PREFIX}/share/dbus-1/system-services/org.octopi.pacmanhelper.service
}
